package com.proton.carepatchtemp.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.ReportCenter;
import com.proton.carepatchtemp.utils.ui.ToastUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONException;
import org.json.JSONObject;

public class AddNoteActivity extends BaseActivity {
    private String drug = "";
    private String phys = "";
    private String cons = "";
    private Long reportId;
    /**
     * 显示的文字
     */
    private String[] drugs = new String[]{
            VCare.get().getString(R.string.note_drug_ibuprofen),
            VCare.get().getString(R.string.note_drug_paracetamol),
            VCare.get().getString(R.string.note_drug_dextro_ibuprofen_suppository),
            VCare.get().getString(R.string.note_drug_999_cold_medicine)
    };

    private String[] physical = new String[]{
            VCare.get().getString(R.string.note_phy_ice_bag),
            VCare.get().getString(R.string.note_phy_warm_bath),
            VCare.get().getString(R.string.note_phy_alcohol_sponge_bath),
            VCare.get().getString(R.string.note_phy_feet_in_warm_water),
            VCare.get().getString(R.string.note_phy_cooling_gel),
            VCare.get().getString(R.string.note_phy_drink_water)
    };

    private String[] condition = new String[]{
            VCare.get().getString(R.string.note_con_coughed),
            VCare.get().getString(R.string.note_con_dehydrated),
            VCare.get().getString(R.string.note_con_twitchy),
            VCare.get().getString(R.string.note_con_unconcious),
            VCare.get().getString(R.string.note_con_general_weakness),
            VCare.get().getString(R.string.note_con_lethargic),
            VCare.get().getString(R.string.note_con_diarrheic),
            VCare.get().getString(R.string.note_con_vomitive),
            VCare.get().getString(R.string.note_con_snotty),
            VCare.get().getString(R.string.note_con_inappetent),
            VCare.get().getString(R.string.note_con_crying),
            VCare.get().getString(R.string.note_con_cold_limbs)
    };

    private JSONObject jsonContent;
    private TagAdapter<String> mDrugAdapter;
    private TagAdapter<String> mPhyAdapter;
    private TagAdapter<String> mConAdapter;
    /**
     * 添加一点备注？
     */
    private EditText mEtNoteComment;
    private TagFlowLayout mFlowDrugTreatment;
    private TagFlowLayout mFlowPhysicalTherapy;
    private TagFlowLayout mFlowPhysicalCondition;
    private ImageButton mBtnBack;
    /**
     * 保存
     */
    private TextView mTvSaveNote;


    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.REPORT_ADD_NOTE:
                if (event.isSuccess()) {
                    ToastUtils.s(R.string.add_note_success);
                    finish();
//                    mFlowDrugTreatment.onChanged();
//                    mFlowDrugTreatment.getSelectedList().clear();
//
//                    mFlowPhysicalCondition.onChanged();
//                    mFlowPhysicalCondition.getSelectedList().clear();
//
//                    mFlowPhysicalTherapy.onChanged();
//                    mFlowPhysicalTherapy.getSelectedList().clear();
//
//                    drug = "";
//                    phys = "";
//                    cons = "";
//                    mEtNoteComment.setText("");
                } else {
                    ToastUtils.e(R.string.add_note_failed);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_note);
        if (!getIntent().hasExtra(Extras.REPORTID) || 0 == getIntent().getLongExtra(Extras.REPORTID, 0)) {
            ToastUtils.e(R.string.get_report_failed);
            finish();
        } else {
            reportId = getIntent().getLongExtra(Extras.REPORTID, 0);
        }

        initView();
        initTabView();
        setListener();

        jsonContent = new JSONObject();
    }

    private void initTabView() {
        mFlowDrugTreatment.setAdapter(mDrugAdapter = new TagAdapter<String>(drugs) {

            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(AddNoteActivity.this).inflate(R.layout.tab_tv,
                        mFlowDrugTreatment, false);
                tv.setText(s);
                return tv;
            }
        });

        mFlowPhysicalTherapy.setAdapter(mPhyAdapter = new TagAdapter<String>(physical) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(AddNoteActivity.this).inflate(R.layout.tab_tv,
                        mFlowPhysicalTherapy, false);
                tv.setText(s);
                return tv;
            }
        });

        mFlowPhysicalCondition.setAdapter(mConAdapter = new TagAdapter<String>(condition) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(AddNoteActivity.this).inflate(R.layout.tab_tv,
                        mFlowPhysicalCondition, false);
                tv.setText(s);
                return tv;
            }
        });
    }

    private void canBack() {
        if (!TextUtils.isEmpty(drug) || !TextUtils.isEmpty(phys) || !TextUtils.isEmpty(cons) || !TextUtils.isEmpty(mEtNoteComment.getText())) {
            AlertDialog.Builder ab = new AlertDialog.Builder(AddNoteActivity.this);
            ab.setCancelable(false);
            ab.setIcon(R.mipmap.ic_launcher);
            ab.setTitle(getString(R.string.string_tips));
            ab.setMessage(R.string.string_not_save_notes);
            ab.setNegativeButton(getString(R.string.string_cancel), (dialog, which) -> {

            });
            ab.setPositiveButton(getString(R.string.string_confirm), (dialog, which) -> {
                finish();
            });

            ab.create().show();
        } else {
            finish();
        }
    }

    @Override
    protected void setListener() {
        mBtnBack.setOnClickListener(v -> {
            canBack();
        });

        mTvSaveNote.setOnClickListener(v -> {

            if (TextUtils.isEmpty(drug) && TextUtils.isEmpty(phys) && TextUtils.isEmpty(cons) && TextUtils.isEmpty(mEtNoteComment.getText())) {
                ToastUtils.w(R.string.string_no_content_add);
                return;
            }

            try {
                if (!TextUtils.isEmpty(drug) || !TextUtils.isEmpty(phys) || !TextUtils.isEmpty(cons) || !TextUtils.isEmpty(mEtNoteComment.getText())) {
                    if (!TextUtils.isEmpty(drug)) {
                        jsonContent.put("1", drug);
                    }
                    if (!TextUtils.isEmpty(phys)) {
                        jsonContent.put("2", phys);
                    }
                    if (!TextUtils.isEmpty(cons)) {
                        jsonContent.put("3", cons);
                    }
                    if (!TextUtils.isEmpty(mEtNoteComment.getText())) {
                        jsonContent.put("7", mEtNoteComment.getText().toString());
                    }
                }
            } catch (JSONException e) {
                LogUtils.e(e);
            }
            LogUtils.d(jsonContent);
            ReportCenter.addComments(reportId, jsonContent.toString());
        });

        mFlowDrugTreatment.setOnSelectListener(selectPosSet -> {
            drug = "";
            for (int p :
                    selectPosSet) {
                drug += drugs[p] + ";";
            }
            LogUtils.d(drug);
        });

        mFlowPhysicalTherapy.setOnSelectListener(selectPosSet -> {
            phys = "";
            for (int p :
                    selectPosSet) {
                phys += physical[p] + ";";
            }
            LogUtils.d(phys);
        });

        mFlowPhysicalCondition.setOnSelectListener(selectPosSet -> {
            cons = "";
            for (int p :
                    selectPosSet) {
                cons += condition[p] + ";";
            }
            LogUtils.d(cons);
        });

    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private void initView() {
        mEtNoteComment = findViewById(R.id.et_note_comment);
        mFlowDrugTreatment = findViewById(R.id.flow_drug_treatment);
        mFlowPhysicalTherapy = findViewById(R.id.flow_physical_therapy);
        mFlowPhysicalCondition = findViewById(R.id.flow_physical_condition);
        mBtnBack = findViewById(R.id.btn_back);
        mTvSaveNote = findViewById(R.id.tv_save_note);
    }

    @Override
    public void onBackPressed() {
        canBack();
    }
}
